package com.xunyji.demo04.web;

import com.xunyji.demo04.model.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author AltEnter
 * @date 2019年1月24日11:24:39
 */
@Controller
public class UserController {

    /**
     * 登录认证逻辑
     * @param user
     * @return
     */
    @RequestMapping(value ="/subLogin", method = RequestMethod.POST, produces = "application/json;charset=utf-8")  //method = RequestMethod.POST,
    @ResponseBody
    public String subLogin(User user){
//        01 获取认证主题
        Subject subject = SecurityUtils.getSubject();
//        02 封装认证token
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
        try {
//            token.setRememberMe(user.isRemenberMe());
//            03 进行登录认证逻辑
            subject.login(token);
        } catch (AuthenticationException e){
            return e.getMessage();
        }

        if(subject.hasRole("admin")){
            return "有admin权限";
        }
        return "有admin权限";
//        return "登录成功";
    }

//    @RequiresRoles("admin")
//    @ResponseBody
//    @RequestMapping(value="/testRole",method = RequestMethod.GET)
//    public String testRole(){
//        return "testRole";
//    }

////    验证角色
//    @RequiresRoles("admin1")
//    @ResponseBody
//    @RequestMapping(value="/testRole1",method = RequestMethod.GET)
//    public String testRole1(){
//        return "testRole1";
//    }
//
//
////    验证权限
//    @RequiresPermissions("user:update")
//    @ResponseBody
//    @RequestMapping(value="/testPesim",method = RequestMethod.GET)
//    public String testPesim(){
//        return "testPesim";
//    }
//
//    @RequiresPermissions("user:update1")
//    @ResponseBody
//    @RequestMapping(value="/testPesim1",method = RequestMethod.GET)
//    public String testPesim1(){
//        return "testPesim1";
//    }

//    @RequiresRoles("admin")
    @ResponseBody
    @RequestMapping(value="/testRole",method = RequestMethod.GET)
    public String testRole(){
        return "testRole";
    }

    //    验证角色
//    @RequiresRoles("admin1")
    @ResponseBody
    @RequestMapping(value="/testRole1",method = RequestMethod.GET)
    public String testRole1(){
        return "testRole1";
    }


    //    验证权限
//    @RequiresPermissions("user:update")
    @ResponseBody
    @RequestMapping(value="/testPesim",method = RequestMethod.GET)
    public String testPesim(){
        return "testPesim";
    }

//    @RequiresPermissions("user:update1")
    @ResponseBody
    @RequestMapping(value="/testPesim1",method = RequestMethod.GET)
    public String testPesim1(){
        return "testPesim1";
    }
}
